/*
 * ViewPatientLogWindow.java
 *
 * Created on 08 4, 11, 6:11:29 AM
 */

package dcds2;

import javax.swing.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Mesan
 */
public class ViewPatientLogWindow extends javax.swing.JFrame {
    
    int transactionID;
    int procedureNo = 1;
    int tableRows = 0;
    private DefaultTableModel modelProcLog = new DefaultTableModel(new Object[][]{}, new String[] {"Date", "Procedure","Dentist","Price"});
    
    /** Creates new form ViewPatientLogWindow */
    public int sPatient;
    public ViewPatientLogWindow(int patientID) {
        sPatient = patientID;
        initComponents();
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setLocationRelativeTo(null);
        
        try{
         ConnectionFactory myFactory = ConnectionFactory.getFactory();
         Connection conn = myFactory.getConnection();
         boolean result = false;
         
         PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.patient WHERE patientID=" +sPatient);

         ResultSet rs = pstmt.executeQuery();
        
        while(rs.next()){
            
            String pI = rs.getString("patientID");
            String fName = rs.getString("pFirstName");
            String mName = rs.getString("pMiddleName");
            String lName = rs.getString("pLastName");
            String pName = lName + ", " + fName + ", " + mName;
            String bDay = rs.getString("pBirthMonth") + " " + rs.getInt("pBirthDay") + ", " + rs.getInt("pBirthYear");
            String gender = rs.getString("pGender");
            
            jTextField1.setText(pI);
            jTextField2.setText(pName);
            jTextField3.setText(bDay);
            jTextField4.setText(gender);
        
        }
        conn.close();
        result = true;
        }catch(Exception e){
        e.printStackTrace();
         }
        
        //for the procedures
        
        try{
         ConnectionFactory myFactory = ConnectionFactory.getFactory();
         Connection conn = myFactory.getConnection();
         boolean result = false;
         
         PreparedStatement pstmt1 = conn.prepareStatement("SELECT dcds.transprocdetails.procedureName AS procName, dcds.transprocdetails.dentistName AS dentName, dcds.transprocdetails.procedurePrice AS procPrice, dcds.transactions.transactionDate AS date FROM dcds.transprocdetails INNER JOIN dcds.transactions ON dcds.transprocdetails.transactionID = dcds.transactions.transactionID WHERE patientID = '"+sPatient+"'");
         //PreparedStatement pstmt1 = conn.prepareStatement("SELECT procedureName, dentistName, procedurePrice FROM dcds.transprocdetails WHERE dcds.transprocdetails.transactionID in (SELECT dcds.transactions.transactionID FROM dcds.transactions WHERE dcds.transactions.patientID = '"+sPatient+"'");
         ResultSet rs1 = pstmt1.executeQuery();
         
         transactionID = rs1.getRow() + 1;
        
         int i = 0;
         while(rs1.next()){
            
            String date = rs1.getString("date");
            String procName = rs1.getString("procName");
            String dentName = rs1.getString("dentName");
            String procPrice = rs1.getString("procPrice");
            modelProcLog.addRow(new Object[] {"","","","",});
            jTable1.setValueAt(date, i, 0);
            jTable1.setValueAt(procName, i, 1);
            jTable1.setValueAt(dentName, i, 2);
            jTable1.setValueAt(procPrice, i, 3);
            
            
            i++; 
        }
   
        conn.close();
        result = true;
        }catch(Exception e){
        e.printStackTrace();
         }
    }

 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblPatientNo = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblBirthdate = new javax.swing.JLabel();
        lblGender = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - Patient Log");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        lblPatientNo.setFont(new java.awt.Font("Century", 0, 12));
        lblPatientNo.setText("Patient No.:");

        lblName.setFont(new java.awt.Font("Century", 0, 12));
        lblName.setText("Name:");

        lblBirthdate.setFont(new java.awt.Font("Century", 0, 12));
        lblBirthdate.setText("Birthdate:");

        lblGender.setFont(new java.awt.Font("Century", 0, 12));
        lblGender.setText("Gender:");

        jTextField1.setEditable(false);

        jTextField2.setEditable(false);
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jTextField3.setEditable(false);
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jTextField4.setEditable(false);

        jTable1.setModel(modelProcLog);
        jTable1.setCellSelectionEnabled(false);
        jTable1.setEnabled(false);
        jTable1.setRowSelectionAllowed(true);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        btnBack.setFont(new java.awt.Font("Century", 0, 11));
        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnBack.setBorder(null);
        btnBack.setBorderPainted(false);
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblPatientNo)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblName)
                            .addComponent(lblBirthdate)
                            .addComponent(lblGender))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(37, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(324, Short.MAX_VALUE)
                .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPatientNo)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBirthdate)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblGender)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        dispose();
        new ViewPatientWindow(sPatient).setVisible(true);
}//GEN-LAST:event_btnBackActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextField2ActionPerformed

    /**
    * @param args the command line arguments
    */
 /*   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewPatientLogWindow().setVisible(true);
            }
        });
    }
*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JLabel lblBirthdate;
    private javax.swing.JLabel lblGender;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPatientNo;
    // End of variables declaration//GEN-END:variables

}

